package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.mapper.SkuFullReductionMapper;
import com.atguigu.gmall.sms.mapper.SkuLadderMapper;
import com.atguigu.gmall.sms.vo.ItemSaleVo;
import com.atguigu.gmall.sms.vo.SkuSaveVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.sms.mapper.SkuBoundsMapper;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {

    @Resource
    private SkuFullReductionMapper skuFullReductionMapper;
    @Resource
    private SkuLadderMapper skuLadderMapper;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSales(SkuSaveVo skuSaveVo) {
        //保存积分优惠sms_sku_bounds
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        BeanUtils.copyProperties(skuSaveVo,skuBoundsEntity);
        List<Integer> work = skuSaveVo.getWork();
        if (CollectionUtils.isNotEmpty(work) && work.size() == 4){
            skuBoundsEntity.setWork(work.get(3) * 8 + work.get(2) * 4 + work.get(1) * 2 + work.get(0));//十进制化
        }
        this.save(skuBoundsEntity);

        //保存满减信息sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuSaveVo,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuSaveVo.getFullAddOther());
        skuFullReductionMapper.insert(skuFullReductionEntity);

        //保存打折信息sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuSaveVo,skuLadderEntity);
        skuLadderEntity.setAddOther(skuSaveVo.getLadderAddOther());
        skuLadderMapper.insert(skuLadderEntity);
    }

    @Override
    public List<ItemSaleVo> querySalesBySkuId(Long skuId) {
        List itemSales = new ArrayList<>();
        //根据skuId查询积分优惠
        SkuBoundsEntity skuBoundsEntity =
                this.getOne(new LambdaQueryWrapper<SkuBoundsEntity>().eq(SkuBoundsEntity::getSkuId, skuId));
        if (skuBoundsEntity != null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuBoundsEntity.getId());
            itemSaleVo.setType("积分");
            itemSaleVo.setDesc("送"+skuBoundsEntity.getGrowBounds()+"成长积分，送"+skuBoundsEntity.getBuyBounds()+"购物积分");
            itemSales.add(itemSaleVo);
        }
        //根据skuId查询满减优惠
        SkuFullReductionEntity skuFullReductionEntity =
                this.skuFullReductionMapper.selectOne(new LambdaQueryWrapper<SkuFullReductionEntity>()
                .eq(SkuFullReductionEntity::getSkuId, skuId));
        if (skuFullReductionEntity != null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuFullReductionEntity.getId());
            itemSaleVo.setType("满减");
            itemSaleVo.setDesc("满"+skuFullReductionEntity.getFullPrice()+"减"
                    +skuFullReductionEntity.getReducePrice());
            itemSales.add(itemSaleVo);
        }
        //根据SkuId查询打折优惠
        SkuLadderEntity skuLadderEntity =
                this.skuLadderMapper.selectOne(new LambdaQueryWrapper<SkuLadderEntity>()
                        .eq(SkuLadderEntity::getSkuId, skuId));
        if (skuBoundsEntity != null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuLadderEntity.getId());
            itemSaleVo.setType("打折");
            itemSaleVo.setDesc("满"+skuLadderEntity.getFullCount()+"打"
                    +skuLadderEntity.getDiscount().divide(new BigDecimal(10))+"折");
            itemSales.add(itemSaleVo);
        }
        return itemSales;
    }
}